# -*- coding: UTF-8 -*- 
from models import *
from forms import *
from django import forms
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from django.template.loader import get_template
from django.core.paginator import Paginator
from django.core.urlresolvers import reverse
import datetime
from django.shortcuts import render_to_response
from django.utils import simplejson
from django.db import connection


def save_follow(request, id):
    tid = id
    print '0000'
    form = FollowForm(request.POST or None)
    print '1111'
    if form is not None:
        print '2222'+id
        if form.is_valid():
            print '3333'
            tssddi = form.cleaned_data['ssddi']
            tssdi = form.cleaned_data['ssdi']
            tessdai=form.cleaned_data['essdai']
            tesspri=form.cleaned_data['esspri']
            tPCS=form.cleaned_data['PCS']
            tMCS=form.cleaned_data['MCS']
            tpcm=form.cleaned_data['pcm']
            ttongue=form.cleaned_data['tongue']
            tother=form.cleaned_data['other']
            tinputdate=form.cleaned_data['inputdate']
            patient_instance = patient.objects.get(id = tid)            
            newfollow = Follow(
                               ssddi = tssddi,
                               ssdi = tssdi,
                               essdai = tessdai,
                               esspri = tesspri,
                               sf12_pcs = tPCS,
                               sf12_mcs = tMCS,
                               pcm = tpcm,
                               tongue = ttongue,
                               other = tother,
                               inputdate = tinputdate,
                               Patient = patient_instance,
                               )
            print '4444'
            newfollow.save()
            follow_session = request.session.get("follow",None)
            if follow_session is not None:
                del request.session["follow"]
            request.session["follow"] =  newfollow    
   
            essdai_session = request.session.get("essdai",None)
            if essdai_session is not None:
                print '----'
#                em = ESSDAI_model.objects.get(id = essdai_session)

                essdai_session.follow = newfollow
                essdai_session.save()

                
            ssdi_session = request.session.get("ssdi",None)
            if ssdi_session is not None:
                print '----'
#                sm = SSDI_model.objects.get(id = ssdi_session)
                ssdi_session.follow = newfollow
                ssdi_session.save()

                
            ssddi_session = request.session.get("ssddi",None)
            if ssddi_session is not None:
                print ssddi_session
#                ssddim = SSDDI_model.objects.get(id = ssddi_session)
                ssddi_session.follow = newfollow
                ssddi_session.save()
                print connection.queries


            flag = 1
            result = simplejson.dumps(flag)
            return HttpResponse(result)

#            return HttpResponseRedirect("/sle/patient/list/") 
            
            
        
    else:
        form = FollowForm()
    t = get_template('sle/follow_save.html')
    c = RequestContext(request,locals())
    return HttpResponse(t.render(c))


def save_essdai(request):
    form = EssdaiForm(request.POST or None)
    if form is not None:
        if form.is_valid():
            tRadioGroup1 = form.cleaned_data['RadioGroup1']
            tRadioGroup2 = form.cleaned_data['RadioGroup2']
            tRadioGroup3 = form.cleaned_data['RadioGroup3']
            tRadioGroup4 = form.cleaned_data['RadioGroup4']
            tRadioGroup5 = form.cleaned_data['RadioGroup5']
            tRadioGroup6 = form.cleaned_data['RadioGroup6']
            tRadioGroup7 = form.cleaned_data['RadioGroup7']
            tRadioGroup8 = form.cleaned_data['RadioGroup8']
            tRadioGroup9 = form.cleaned_data['RadioGroup9']
            tRadioGroup10 = form.cleaned_data['RadioGroup10']
            tRadioGroup11 = form.cleaned_data['RadioGroup11']
            tRadioGroup12 = form.cleaned_data['RadioGroup12']
            
            newessdai = ESSDAI_model(RadioGroup1 = tRadioGroup1,
                                     RadioGroup2 = tRadioGroup2,
                                     RadioGroup3 = tRadioGroup3,
                                     RadioGroup4 = tRadioGroup4,
                                     RadioGroup5 = tRadioGroup5,
                                     RadioGroup6 = tRadioGroup6,
                                     RadioGroup7 = tRadioGroup7,
                                     RadioGroup8 = tRadioGroup8,
                                     RadioGroup9 = tRadioGroup9,
                                     RadioGroup10 = tRadioGroup10,
                                     RadioGroup11 = tRadioGroup11,
                                     RadioGroup12 = tRadioGroup12,
                                     )
#            newessdai.save()
            essdai_session = request.session.get("essdai",None)
            if essdai_session is not None:
                del request.session['essdai']
            
            request.session["essdai"] = newessdai
            
            
            
            flag = 1
            result = simplejson.dumps(flag)
            return HttpResponse(result)

#            return HttpResponseRedirect("/sle/patient/list/")
            
            
        
    else:
        form = EssdaiForm()
    flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)
    

def save_ssdi(request):
    form = SsdiForm(request.POST or None)
    if form is not None:
        if form.is_valid():
            
            tRadioGroup1 = form.cleaned_data['RadioGroup1']
            tRadioGroup2 = form.cleaned_data['RadioGroup2']
            tRadioGroup3 = form.cleaned_data['RadioGroup3']
            tRadioGroup4 = form.cleaned_data['RadioGroup4']
            tRadioGroup5 = form.cleaned_data['RadioGroup5']
            tRadioGroup6 = form.cleaned_data['RadioGroup6']
            tRadioGroup7 = form.cleaned_data['RadioGroup7']
            tRadioGroup8 = form.cleaned_data['RadioGroup8']
            tRadioGroup9 = form.cleaned_data['RadioGroup9']
            ttext_r = form.cleaned_data['text_r']
            ttext_l = form.cleaned_data['text_l']
            ttext_12 = form.cleaned_data['text_12']
            tRadioGroup13 = form.cleaned_data['RadioGroup13']
            tRadioGroup14 = form.cleaned_data['RadioGroup14']
            tRadioGroup15 = form.cleaned_data['RadioGroup15']
            tRadioGroup16 = form.cleaned_data['RadioGroup16']
            tRadioGroup17 = form.cleaned_data['RadioGroup17']
            tRadioGroup18 = form.cleaned_data['RadioGroup18']
            tRadioGroup19 = form.cleaned_data['RadioGroup19']
            tRadioGroup20 = form.cleaned_data['RadioGroup20']
            tRadioGroup21 = form.cleaned_data['RadioGroup21']
            tRadioGroup22 = form.cleaned_data['RadioGroup22']
            tRadioGroup23 = form.cleaned_data['RadioGroup23']
            tRadioGroup24 = form.cleaned_data['RadioGroup24']
            tRadioGroup25 = form.cleaned_data['RadioGroup25']
            tRadioGroup26 = form.cleaned_data['RadioGroup26']
            tRadioGroup27 = form.cleaned_data['RadioGroup27']
            tRadioGroup28 = form.cleaned_data['RadioGroup28']
            tRadioGroup29 = form.cleaned_data['RadioGroup29']
            tRadioGroup30 = form.cleaned_data['RadioGroup30']
            tRadioGroup31 = form.cleaned_data['RadioGroup31']
            tRadioGroup32 = form.cleaned_data['RadioGroup32']
            tRadioGroup33 = form.cleaned_data['RadioGroup33']
            tRadioGroup34 = form.cleaned_data['RadioGroup34']
            tRadioGroup35 = form.cleaned_data['RadioGroup35']
            tRadioGroup36 = form.cleaned_data['RadioGroup36']
            tRadioGroup37 = form.cleaned_data['RadioGroup37']
            tRadioGroup38 = form.cleaned_data['RadioGroup38']
            tRadioGroup39 = form.cleaned_data['RadioGroup39']
            tRadioGroup40 = form.cleaned_data['RadioGroup40']

            tdd = form.cleaned_data['dd']
            tzcfw_dd = form.cleaned_data['zcfw_dd']
            tt4 = form.cleaned_data['t4']
            tzcfw_t4 = form.cleaned_data['zcfw_t4']
            ttsh = form.cleaned_data['tsh']
            tzcfw_tsh = form.cleaned_data['zcfw_tsh']
            

            newssdi = SSDI_model(RadioGroup1 = tRadioGroup1,
                                     RadioGroup2 = tRadioGroup2,
                                     RadioGroup3 = tRadioGroup3,
                                     RadioGroup4 = tRadioGroup4,
                                     RadioGroup5 = tRadioGroup5,
                                     RadioGroup6 = tRadioGroup6,
                                     RadioGroup7 = tRadioGroup7,
                                     RadioGroup8 = tRadioGroup8,
                                     RadioGroup9 = tRadioGroup9,
                                     RadioGroup13 = tRadioGroup13,
                                     RadioGroup14 = tRadioGroup14,
                                     RadioGroup15 = tRadioGroup15,
                                     RadioGroup16 = tRadioGroup16,
                                     RadioGroup17 = tRadioGroup17,
                                     RadioGroup18 = tRadioGroup18,
                                     RadioGroup19 = tRadioGroup19,
                                     RadioGroup20 = tRadioGroup20,
                                     RadioGroup21 = tRadioGroup21,
                                     RadioGroup22 = tRadioGroup22,
                                     RadioGroup23 = tRadioGroup23,
                                     RadioGroup24 = tRadioGroup24,
                                     RadioGroup25 = tRadioGroup25,
                                     RadioGroup26 = tRadioGroup26,
                                     RadioGroup27 = tRadioGroup27,
                                     RadioGroup28 = tRadioGroup28,
                                     RadioGroup29 = tRadioGroup29,
                                     RadioGroup30 = tRadioGroup30,
                                     RadioGroup31 = tRadioGroup31,
                                     RadioGroup32 = tRadioGroup32,
                                     RadioGroup33 = tRadioGroup33,
                                     RadioGroup34 = tRadioGroup34,
                                     RadioGroup35 = tRadioGroup35,
                                     RadioGroup36 = tRadioGroup36,
                                     RadioGroup37 = tRadioGroup37,
                                     RadioGroup38 = tRadioGroup38,
                                     RadioGroup39 = tRadioGroup39,
                                     RadioGroup40 = tRadioGroup40,
                                     text_R = ttext_r,
                                     text_L = ttext_l,
                                     text_12 = ttext_12,
                                     DD = tdd,
                                     ZCFW_DD = tzcfw_dd,
                                     T4 = tt4,
                                     ZCFW_T4 = tzcfw_t4,
                                     TSH = ttsh,
                                     ZCFW_TSH = tzcfw_tsh,
                                     )
            total = int(tRadioGroup1)+int(tRadioGroup2)+int(tRadioGroup3)+int(tRadioGroup4)+int(tRadioGroup5)+int(tRadioGroup6)+int(tRadioGroup7)+int(tRadioGroup8)+int(tRadioGroup9)+int(tRadioGroup13)+int(tRadioGroup14)+int(tRadioGroup15)+int(tRadioGroup16)+int(tRadioGroup17)+int(tRadioGroup18)+int(tRadioGroup19)+int(tRadioGroup20)+int(tRadioGroup21)+int(tRadioGroup22)+int(tRadioGroup23)+int(tRadioGroup24)+int(tRadioGroup25)+int(tRadioGroup26)+int(tRadioGroup27)+int(tRadioGroup28)+int(tRadioGroup29)+int(tRadioGroup30)+int(tRadioGroup31)+int(tRadioGroup32)+int(tRadioGroup33)+int(tRadioGroup34)+int(tRadioGroup35)+int(tRadioGroup36)+int(tRadioGroup37)+int(tRadioGroup38)+int(tRadioGroup39)+int(tRadioGroup40)
#            newssdi.save()
            
            
            ssdi_session = request.session.get("ssdi",None)
            if ssdi_session is not None:
                del request.session['ssdi']
            
            request.session["ssdi"] =  newssdi
            
            
            flag = {'flag':1,'total':total}
            result = simplejson.dumps(flag)
            return HttpResponse(result)

#            return HttpResponseRedirect("/sle/patient/list/")
            
            
        
    else:
        form = SsdiForm()
    flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)


def save_ssddi(request):
    form = SsddiForm(request.POST or None)
    if form is not None:
        if form.is_valid():
            tcheckbox1 = form.cleaned_data['checkbox1']
            tcheckbox2 = form.cleaned_data['checkbox2']
            tcheckbox3 = form.cleaned_data['checkbox3']
            tcheckbox4 = form.cleaned_data['checkbox4']
            tcheckbox5 = form.cleaned_data['checkbox5']
            tcheckbox6 = form.cleaned_data['checkbox6']
            tcheckbox7 = form.cleaned_data['checkbox7']
            tcheckbox8 = form.cleaned_data['checkbox8']
            tcheckbox9 = form.cleaned_data['checkbox9']
            tcheckbox10 = form.cleaned_data['checkbox10']
            tcheckbox11 = form.cleaned_data['checkbox11']
            tcheckbox12 = form.cleaned_data['checkbox12']
            tcheckbox13 = form.cleaned_data['checkbox13']
            tcheckbox14 = form.cleaned_data['checkbox14']
            tcheckbox15 = form.cleaned_data['checkbox15']
            
            newssddi = SSDDI_model(checkbox1 = tcheckbox1,
                                     checkbox2 = tcheckbox2,
                                     checkbox3 = tcheckbox3,
                                     checkbox4 = tcheckbox4,
                                     checkbox5 = tcheckbox5,
                                     checkbox6 = tcheckbox6,
                                     checkbox7 = tcheckbox7,
                                     checkbox8 = tcheckbox8,
                                     checkbox9 = tcheckbox9,
                                     checkbox10 = tcheckbox10,
                                     checkbox11 = tcheckbox11,
                                     checkbox12 = tcheckbox12,
                                     checkbox13 = tcheckbox13,
                                     checkbox14 = tcheckbox14,
                                     checkbox15 = tcheckbox15,
                                     )
#            newssddi.save()
            ssddi_session = request.session.get("ssddi",None)
            if ssddi_session is not None:
                del request.session['ssddi']
            
            request.session["ssddi"] =  newssddi
            
            flag = 1
            result = simplejson.dumps(flag)
            return HttpResponse(result)

#            return HttpResponseRedirect("/sle/patient/list/")
            
            
        
    else:
        form = SsddiForm()
    flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)


def save_labfindings(request):
    print '000'
    form = LabfindingsForm(request.POST or None)
    print '111'
    if form is not None:
        print '222'
        if form.is_valid():
            print '333'
            tANA = form.cleaned_data['ANA']
            tanti_ENA_Ab = form.cleaned_data['anti_ENA_Ab']
            tSSA = form.cleaned_data['SSA']
            tSSB = form.cleaned_data['SSB']
            tRNP = form.cleaned_data['RNP']
            tJo = form.cleaned_data['Jo']
            tScl = form.cleaned_data['Scl']
            tdsDNA = form.cleaned_data['dsDNA']
            tMPO = form.cleaned_data['MPO']
            tPR3 = form.cleaned_data['PR3']
            tanti_CCP_Ab = form.cleaned_data['anti_CCP_Ab']
            tanticardiolipin = form.cleaned_data['anticardiolipin']
            tRF = form.cleaned_data['RF']
            tserum = form.cleaned_data['serum']
            tIgM = form.cleaned_data['IgM']
            tIgA = form.cleaned_data['IgA']
            tC3 = form.cleaned_data['C3']
            tC4 = form.cleaned_data['C4']
            tCH50 = form.cleaned_data['CH50']
            thormone = form.cleaned_data['hormone']
            tSS = form.cleaned_data['SS']
            tCTX = form.cleaned_data['CTX']
            tMTX = form.cleaned_data['MTX']
            tAza = form.cleaned_data['Aza']
            tCQ = form.cleaned_data['CQ']
            treaction = form.cleaned_data['reaction']
            tX_ray = form.cleaned_data['X_ray']
            tCTScan = form.cleaned_data['CTScan']
            tECG = form.cleaned_data['ECG']
            tUCG = form.cleaned_data['UCG']
            tPulmonary = form.cleaned_data['Pulmonary']
            tEEG = form.cleaned_data['EEG']
            tultrasonagraphy = form.cleaned_data['ultrasonagraphy']
            tEMG = form.cleaned_data['EMG']
            tLabial = form.cleaned_data['Labial']
            tpathologyNO1 = form.cleaned_data['pathologyNO1']
            tdate1 = form.cleaned_data['date1']
            tRenal = form.cleaned_data['Renal']
            tpathologyNO2 = form.cleaned_data['pathologyNO2']
            tdate2 = form.cleaned_data['date2']
            tBone = form.cleaned_data['Bone']
            tpathologyNO3 = form.cleaned_data['pathologyNO3']
            tdate3 = form.cleaned_data['date3']
            tother = form.cleaned_data['other']
            tinputdate = form.cleaned_data['inputdate']
            tpatient_id = form.cleaned_data['patient_id']
            print '444'
            tpatient = patient.objects.get(id = tpatient_id)
            print '555'
            newlabfindings = Labfindings(
                                         ANA = tANA,
                                         anti_ENA_Ab = tanti_ENA_Ab,
                                         SSA = tSSA,
                                         SSB = tSSB,
                                         RNP = tRNP,
                                         Jo = tJo,
                                         Scl = tScl,
                                         dsDNA = tdsDNA,
                                         MPO = tMPO,
                                         PR3 = tPR3,
                                         anti_CCP_Ab = tanti_CCP_Ab,
                                         anticardiolipin = tanticardiolipin,
                                         RF = tRF,
                                         serum = tserum,
                                         IgM = tIgM,
                                         IgA = tIgA,
                                         C3 = tC3,
                                         C4 = tC4,
                                         CH50 = tCH50,
                                         hormone = thormone,
                                         SS = tSS,
                                         CTX = tCTX,
                                         MTX = tMTX,
                                         Aza = tAza,
                                         CQ = tCQ,
                                         reaction = treaction,
                                         X_ray = tX_ray,
                                         CTScan = tCTScan,
                                         ECG = tECG,
                                         UCG = tUCG,
                                         Pulmonary = tPulmonary,
                                         EEG = tEEG,
                                         ultrasonagraphy = tultrasonagraphy,
                                         EMG = tEMG,
                                         Labial = tLabial,
                                         pathologyNO1 = tpathologyNO1,
                                         date1 = tdate1,
                                         Renal = tRenal,
                                         pathologyNO2 = tpathologyNO2,
                                         date2 = tdate2,
                                         Bone = tBone,
                                         pathologyNO3 = tpathologyNO3,
                                         date3 = tdate3,
                                         other = tother,
                                         inputdate = tinputdate,
                                         Patient = tpatient,
                                         Follow = request.session.get("follow",None),                     
                                         )
            print '666'
            newlabfindings.save()
            print '777'
            flag = 1
            result = simplejson.dumps(flag)
#            return HttpResponse(result)

            return HttpResponseRedirect("/sle/patient/list/")
            
            
        
    else:
        form = LabfindingsForm()
    flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)
            
def list_follow(request,id):

    list_items = Follow.objects.filter(Patient_id = id).order_by('-id')
    paginator = Paginator(list_items ,10)


    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    try:
        list_items = paginator.page(page)
    except :
        list_items = paginator.page(paginator.num_pages)

    t = get_template('sle/list_follow.html')
    c = RequestContext(request,locals())
    return HttpResponse(t.render(c))            
            
            
            
def del_follow(request,patientid,id):
    
    Follow.objects.filter(id=id).delete()
    
    list_items = Follow.objects.filter(Patient_id = patientid).order_by('-id')
    id = patientid
    paginator = Paginator(list_items ,10)

    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    try:
        list_items = paginator.page(page)
    except :
        list_items = paginator.page(paginator.num_pages)

    t = get_template('sle/list_follow.html')
    c = RequestContext(request,locals())
    return HttpResponse(t.render(c))

def view_follow(request,id):
    follow_instance = Follow.objects.get(id = id)
    Labfindings_instance = Labfindings.objects.filter(Follow = follow_instance)
    ESSDAI_model_instance = ESSDAI_model.objects.get(follow = follow_instance)
    SSDI_model_instance = SSDI_model.objects.get(follow = follow_instance)
    SSDDI_model_instance  = SSDDI_model.objects.get(follow = follow_instance)
    
    t=get_template('sle/follow_edit.html')
    c=RequestContext(request,locals())
    return HttpResponse(t.render(c))

def updata_follow(request,id):
    form = FollowForm(request.POST or None)
    if form is not None:
        print '2222'+id
        if form.is_valid():
            print '3333'
            tssddi = form.cleaned_data['ssddi']
            tssdi = form.cleaned_data['ssdi']
            tessdai=form.cleaned_data['essdai']
            tesspri=form.cleaned_data['esspri']
            tPCS=form.cleaned_data['PCS']
            tMCS=form.cleaned_data['MCS']
            tpcm=form.cleaned_data['pcm']
            ttongue=form.cleaned_data['tongue']
            tother=form.cleaned_data['other']
            tinputdate=form.cleaned_data['inputdate']
            
            newfollow = Follow.objects.get(id = id)
            
            newfollow.ssddi = tssddi
            newfollow.ssdi = tssdi
            newfollow.essdai = tessdai
            newfollow.sf12_pcs = tPCS
            newfollow.sf12_mcs = tMCS
            newfollow.esspri = tesspri
            newfollow.pcm = tpcm
            newfollow.tongue = ttongue
            newfollow.other = tother
            newfollow.inputdate = tinputdate
            
            newfollow.save()
            flag = 1
            result = simplejson.dumps(flag)
            return HttpResponse(result)
    else:
        flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)


def updata_labfindings(request,id):
    print '000'
    form = LabfindingsForm(request.POST or None)
    print '111'
    if form is not None:
        print '222'
        if form.is_valid():
            print '333'
            tANA = form.cleaned_data['ANA']
            tanti_ENA_Ab = form.cleaned_data['anti_ENA_Ab']
            tSSA = form.cleaned_data['SSA']
            tSSB = form.cleaned_data['SSB']
            tRNP = form.cleaned_data['RNP']
            tJo = form.cleaned_data['Jo']
            tScl = form.cleaned_data['Scl']
            tdsDNA = form.cleaned_data['dsDNA']
            tMPO = form.cleaned_data['MPO']
            tPR3 = form.cleaned_data['PR3']
            tanti_CCP_Ab = form.cleaned_data['anti_CCP_Ab']
            tanticardiolipin = form.cleaned_data['anticardiolipin']
            tRF = form.cleaned_data['RF']
            tserum = form.cleaned_data['serum']
            tIgM = form.cleaned_data['IgM']
            tIgA = form.cleaned_data['IgA']
            tC3 = form.cleaned_data['C3']
            tC4 = form.cleaned_data['C4']
            tCH50 = form.cleaned_data['CH50']
            thormone = form.cleaned_data['hormone']
            tSS = form.cleaned_data['SS']
            tCTX = form.cleaned_data['CTX']
            tMTX = form.cleaned_data['MTX']
            tAza = form.cleaned_data['Aza']
            tCQ = form.cleaned_data['CQ']
            treaction = form.cleaned_data['reaction']
            tX_ray = form.cleaned_data['X_ray']
            tCTScan = form.cleaned_data['CTScan']
            tECG = form.cleaned_data['ECG']
            tUCG = form.cleaned_data['UCG']
            tPulmonary = form.cleaned_data['Pulmonary']
            tEEG = form.cleaned_data['EEG']
            tultrasonagraphy = form.cleaned_data['ultrasonagraphy']
            tEMG = form.cleaned_data['EMG']
            tLabial = form.cleaned_data['Labial']
            tpathologyNO1 = form.cleaned_data['pathologyNO1']
            tdate1 = form.cleaned_data['date1']
            tRenal = form.cleaned_data['Renal']
            tpathologyNO2 = form.cleaned_data['pathologyNO2']
            tdate2 = form.cleaned_data['date2']
            tBone = form.cleaned_data['Bone']
            tpathologyNO3 = form.cleaned_data['pathologyNO3']
            tdate3 = form.cleaned_data['date3']
            tother = form.cleaned_data['other']
            tinputdate = form.cleaned_data['inputdate']
            tpatient_id = form.cleaned_data['patient_id']

            newlabfindings = Labfindings.objects.get(id = id)

            newlabfindings.ANA = tANA
            newlabfindings.anti_ENA_Ab = tanti_ENA_Ab
            newlabfindings.SSA = tSSA
            newlabfindings.SSB = tSSB
            newlabfindings.RNP = tRNP
            newlabfindings.Jo = tJo
            newlabfindings.Scl = tScl
            newlabfindings.dsDNA = tdsDNA
            newlabfindings.MPO = tMPO
            newlabfindings.PR3 = tPR3
            newlabfindings.anti_CCP_Ab = tanti_CCP_Ab
            newlabfindings.anticardiolipin = tanticardiolipin
            newlabfindings.RF = tRF
            newlabfindings.serum = tserum
            newlabfindings.IgM = tIgM
            newlabfindings.IgA = tIgA
            newlabfindings.C3 = tC3
            newlabfindings.C4 = tC4
            newlabfindings.CH50 = tCH50
            newlabfindings.hormone = thormone
            newlabfindings.SS = tSS
            newlabfindings.CTX = tCTX
            newlabfindings.MTX = tMTX
            newlabfindings.Aza = tAza
            newlabfindings.CQ = tCQ
            newlabfindings.reaction = treaction
            newlabfindings.X_ray = tX_ray
            newlabfindings.CTScan = tCTScan
            newlabfindings.ECG = tECG
            newlabfindings.UCG = tUCG
            newlabfindings.Pulmonary = tPulmonary
            newlabfindings.EEG = tEEG
            newlabfindings.ultrasonagraphy = tultrasonagraphy
            newlabfindings.EMG = tEMG
            newlabfindings.Labial = tLabial
            newlabfindings.pathologyNO1 = tpathologyNO1
            newlabfindings.date1 = tdate1
            newlabfindings.Renal = tRenal
            newlabfindings.pathologyNO2 = tpathologyNO2
            newlabfindings.date2 = tdate2
            newlabfindings.Bone = tBone
            newlabfindings.pathologyNO3 = tpathologyNO3
            newlabfindings.date3 = tdate3
            newlabfindings.other = tother
            newlabfindings.inputdate = tinputdate

            newlabfindings.save()

            flag = 1
            result = simplejson.dumps(flag)
            return HttpResponse(result)

    else:
        form = LabfindingsForm()
    flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)
    
    
    
def updata_ssddi(request,id):
    form = SsddiForm(request.POST or None)
    if form is not None:
        if form.is_valid():
            tcheckbox1 = form.cleaned_data['checkbox1']
            tcheckbox2 = form.cleaned_data['checkbox2']
            tcheckbox3 = form.cleaned_data['checkbox3']
            tcheckbox4 = form.cleaned_data['checkbox4']
            tcheckbox5 = form.cleaned_data['checkbox5']
            tcheckbox6 = form.cleaned_data['checkbox6']
            tcheckbox7 = form.cleaned_data['checkbox7']
            tcheckbox8 = form.cleaned_data['checkbox8']
            tcheckbox9 = form.cleaned_data['checkbox9']
            tcheckbox10 = form.cleaned_data['checkbox10']
            tcheckbox11 = form.cleaned_data['checkbox11']
            tcheckbox12 = form.cleaned_data['checkbox12']
            tcheckbox13 = form.cleaned_data['checkbox13']
            tcheckbox14 = form.cleaned_data['checkbox14']
            tcheckbox15 = form.cleaned_data['checkbox15']
            
            newssddi = SSDDI_model.objects.get(id = id)
            
            newssddi.checkbox1 = tcheckbox1
            newssddi.checkbox2 = tcheckbox2
            newssddi.checkbox3 = tcheckbox3
            newssddi.checkbox4 = tcheckbox4
            newssddi.checkbox5 = tcheckbox5
            newssddi.checkbox6 = tcheckbox6
            newssddi.checkbox7 = tcheckbox7
            newssddi.checkbox8 = tcheckbox8
            newssddi.checkbox9 = tcheckbox9
            newssddi.checkbox10 = tcheckbox10
            newssddi.checkbox11 = tcheckbox11
            newssddi.checkbox12 = tcheckbox12
            newssddi.checkbox13 = tcheckbox13
            newssddi.checkbox14 = tcheckbox14
            newssddi.checkbox15 = tcheckbox15
                                     
            newssddi.save()
            
            flag = 1
            result = simplejson.dumps(flag)
            return HttpResponse(result)
         
        
    else:
        form = SsddiForm()
    flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)    
    

def updata_ssdi(request,id):
    form = SsdiForm(request.POST or None)
    if form is not None:
        if form.is_valid():
            
            tRadioGroup1 = form.cleaned_data['RadioGroup1']
            tRadioGroup2 = form.cleaned_data['RadioGroup2']
            tRadioGroup3 = form.cleaned_data['RadioGroup3']
            tRadioGroup4 = form.cleaned_data['RadioGroup4']
            tRadioGroup5 = form.cleaned_data['RadioGroup5']
            tRadioGroup6 = form.cleaned_data['RadioGroup6']
            tRadioGroup7 = form.cleaned_data['RadioGroup7']
            tRadioGroup8 = form.cleaned_data['RadioGroup8']
            tRadioGroup9 = form.cleaned_data['RadioGroup9']
            ttext_r = form.cleaned_data['text_r']
            ttext_l = form.cleaned_data['text_l']
            ttext_12 = form.cleaned_data['text_12']
            tRadioGroup13 = form.cleaned_data['RadioGroup13']
            tRadioGroup14 = form.cleaned_data['RadioGroup14']
            tRadioGroup15 = form.cleaned_data['RadioGroup15']
            tRadioGroup16 = form.cleaned_data['RadioGroup16']
            tRadioGroup17 = form.cleaned_data['RadioGroup17']
            tRadioGroup18 = form.cleaned_data['RadioGroup18']
            tRadioGroup19 = form.cleaned_data['RadioGroup19']
            tRadioGroup20 = form.cleaned_data['RadioGroup20']
            tRadioGroup21 = form.cleaned_data['RadioGroup21']
            tRadioGroup22 = form.cleaned_data['RadioGroup22']
            tRadioGroup23 = form.cleaned_data['RadioGroup23']
            tRadioGroup24 = form.cleaned_data['RadioGroup24']
            tRadioGroup25 = form.cleaned_data['RadioGroup25']
            tRadioGroup26 = form.cleaned_data['RadioGroup26']
            tRadioGroup27 = form.cleaned_data['RadioGroup27']
            tRadioGroup28 = form.cleaned_data['RadioGroup28']
            tRadioGroup29 = form.cleaned_data['RadioGroup29']
            tRadioGroup30 = form.cleaned_data['RadioGroup30']
            tRadioGroup31 = form.cleaned_data['RadioGroup31']
            tRadioGroup32 = form.cleaned_data['RadioGroup32']
            tRadioGroup33 = form.cleaned_data['RadioGroup33']
            tRadioGroup34 = form.cleaned_data['RadioGroup34']
            tRadioGroup35 = form.cleaned_data['RadioGroup35']
            tRadioGroup36 = form.cleaned_data['RadioGroup36']
            tRadioGroup37 = form.cleaned_data['RadioGroup37']
            tRadioGroup38 = form.cleaned_data['RadioGroup38']
            tRadioGroup39 = form.cleaned_data['RadioGroup39']
            tRadioGroup40 = form.cleaned_data['RadioGroup40']

            tdd = form.cleaned_data['dd']
            tzcfw_dd = form.cleaned_data['zcfw_dd']
            tt4 = form.cleaned_data['t4']
            tzcfw_t4 = form.cleaned_data['zcfw_t4']
            ttsh = form.cleaned_data['tsh']
            tzcfw_tsh = form.cleaned_data['zcfw_tsh']
            newssdi = SSDI_model.objects.get(id = id)
            

            newssdi.RadioGroup1 = tRadioGroup1
            newssdi.RadioGroup2 = tRadioGroup2
            newssdi.RadioGroup3 = tRadioGroup3
            newssdi.RadioGroup4 = tRadioGroup4
            newssdi.RadioGroup5 = tRadioGroup5
            newssdi.RadioGroup6 = tRadioGroup6
            newssdi.RadioGroup7 = tRadioGroup7
            newssdi.RadioGroup8 = tRadioGroup8
            newssdi.RadioGroup9 = tRadioGroup9
            newssdi.RadioGroup13 = tRadioGroup13
            newssdi.RadioGroup14 = tRadioGroup14
            newssdi.RadioGroup15 = tRadioGroup15
            newssdi.RadioGroup16 = tRadioGroup16
            newssdi.RadioGroup17 = tRadioGroup17
            newssdi.RadioGroup18 = tRadioGroup18
            newssdi.RadioGroup19 = tRadioGroup19
            newssdi.RadioGroup20 = tRadioGroup20
            newssdi.RadioGroup21 = tRadioGroup21
            newssdi.RadioGroup22 = tRadioGroup22
            newssdi.RadioGroup23 = tRadioGroup23
            newssdi.RadioGroup24 = tRadioGroup24
            newssdi.RadioGroup25 = tRadioGroup25
            newssdi.RadioGroup26 = tRadioGroup26
            newssdi.RadioGroup27 = tRadioGroup27
            newssdi.RadioGroup28 = tRadioGroup28
            newssdi.RadioGroup29 = tRadioGroup29
            newssdi.RadioGroup30 = tRadioGroup30
            newssdi.RadioGroup31 = tRadioGroup31
            newssdi.RadioGroup32 = tRadioGroup32
            newssdi.RadioGroup33 = tRadioGroup33
            newssdi.RadioGroup34 = tRadioGroup34
            newssdi.RadioGroup35 = tRadioGroup35
            newssdi.RadioGroup36 = tRadioGroup36
            newssdi.RadioGroup37 = tRadioGroup37
            newssdi.RadioGroup38 = tRadioGroup38
            newssdi.RadioGroup39 = tRadioGroup39
            newssdi.RadioGroup40 = tRadioGroup40
            newssdi.text_R = ttext_r
            newssdi.text_L = ttext_l
            newssdi.text_12 = ttext_12
            newssdi.DD = tdd
            newssdi.ZCFW_DD = tzcfw_dd
            newssdi.T4 = tt4
            newssdi.ZCFW_T4 = tzcfw_t4
            newssdi.TSH = ttsh
            newssdi.ZCFW_TSH = tzcfw_tsh

            total = int(tRadioGroup1)+int(tRadioGroup2)+int(tRadioGroup3)+int(tRadioGroup4)+int(tRadioGroup5)+int(tRadioGroup6)+int(tRadioGroup7)+int(tRadioGroup8)+int(tRadioGroup9)+int(tRadioGroup13)+int(tRadioGroup14)+int(tRadioGroup15)+int(tRadioGroup16)+int(tRadioGroup17)+int(tRadioGroup18)+int(tRadioGroup19)+int(tRadioGroup20)+int(tRadioGroup21)+int(tRadioGroup22)+int(tRadioGroup23)+int(tRadioGroup24)+int(tRadioGroup25)+int(tRadioGroup26)+int(tRadioGroup27)+int(tRadioGroup28)+int(tRadioGroup29)+int(tRadioGroup30)+int(tRadioGroup31)+int(tRadioGroup32)+int(tRadioGroup33)+int(tRadioGroup34)+int(tRadioGroup35)+int(tRadioGroup36)+int(tRadioGroup37)+int(tRadioGroup38)+int(tRadioGroup39)+int(tRadioGroup40)
            newssdi.save()           
            
            flag = {'flag':1,'total':total}
            result = simplejson.dumps(flag)
            return HttpResponse(result)

      
    else:
        form = SsdiForm()
    flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)


def updata_essdai(request,id):
    form = EssdaiForm(request.POST or None)
    if form is not None:
        if form.is_valid():
            tRadioGroup1 = form.cleaned_data['RadioGroup1']
            tRadioGroup2 = form.cleaned_data['RadioGroup2']
            tRadioGroup3 = form.cleaned_data['RadioGroup3']
            tRadioGroup4 = form.cleaned_data['RadioGroup4']
            tRadioGroup5 = form.cleaned_data['RadioGroup5']
            tRadioGroup6 = form.cleaned_data['RadioGroup6']
            tRadioGroup7 = form.cleaned_data['RadioGroup7']
            tRadioGroup8 = form.cleaned_data['RadioGroup8']
            tRadioGroup9 = form.cleaned_data['RadioGroup9']
            tRadioGroup10 = form.cleaned_data['RadioGroup10']
            tRadioGroup11 = form.cleaned_data['RadioGroup11']
            tRadioGroup12 = form.cleaned_data['RadioGroup12']
            
            newessdai = ESSDAI_model.objects.get(id = id)
            
            newessdai.RadioGroup1 = tRadioGroup1
            newessdai.RadioGroup2 = tRadioGroup2
            newessdai.RadioGroup3 = tRadioGroup3
            newessdai.RadioGroup4 = tRadioGroup4
            newessdai.RadioGroup5 = tRadioGroup5
            newessdai.RadioGroup6 = tRadioGroup6
            newessdai.RadioGroup7 = tRadioGroup7
            newessdai.RadioGroup8 = tRadioGroup8
            newessdai.RadioGroup9 = tRadioGroup9
            newessdai.RadioGroup10 = tRadioGroup10
            newessdai.RadioGroup11 = tRadioGroup11
            newessdai.RadioGroup12 = tRadioGroup12

            newessdai.save()            
            
            
            flag = 1
            result = simplejson.dumps(flag)
            return HttpResponse(result)
            
        
    else:
        form = EssdaiForm()
    flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)

